package day11;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.junit.Test;

public class JDBCTest2 {

	@Test
	public void testInsert() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO student(`name`,age,gender) VALUES(?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "张三222");
			preparedStatement.setInt(2, 23);
			preparedStatement.setString(3, "女");
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
	}
	
	@Test
	public void testUpdate() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "update student set age=? where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, 30);
			preparedStatement.setInt(2, 9);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("修改成功");
			} else {
				System.out.println("修改失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
	}
	
	
	@Test
	public void testDelete() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, 9);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
	}
	
	
	
	
	public static void main(String[] args) {
//		System.out.println(Integer.MAX_VALUE);
//		String str = "dhfifdEWE";
//		System.out.println("sdhisdhSADS".toUpperCase());
		
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				// 2*9=18
				System.out.print(j + "*" + i + "=" + (i * j) + "\t");
			}
			
			System.out.println();
		}
		// i=1 j=1
		// i=2 j=2
	}
	
	@Test
	public void test1() {
		String goodsName = "诺基亚原装5800耳机";
		String name = "HTC" + goodsName.substring(3);
		System.out.println(name);
	}
}
